package com.taiji.dianthus.controller.app;

import com.taiji.dianthus.common.BasicController;
import com.taiji.dianthus.common.BusinessMessage;
import com.taiji.dianthus.domain.app.AppInfo;
import com.taiji.dianthus.domain.sys.DSysUser;
import com.taiji.dianthus.service.app.AppInfoService;
import com.taiji.dianthus.service.sys.SysLogService;
import com.taiji.dianthus.util.IPUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

import static com.taiji.dianthus.common.Constant.*;

/**
 * @ClassName SysLogController
 * @Description 日志控制类
 * @Author H.M
 * @Date 2020/6/28
 */
@RequestMapping(value = "/app/info")
@Controller
public class AppInfoController extends BasicController {

    @Autowired
    private AppInfoService appInfoService;
    @Autowired
    private SysLogService sysLogService;


    /**
     * 查询应用列表
     *
     * @param request
     * @param model
     * @param pageSize
     * @param currentPage
     * @return
     */
    @RequestMapping(value = "/list")
    public String list(HttpServletRequest request, Model model, Integer pageSize, Integer currentPage, Integer enableFlag, String appName) {
        pageSize = getPageSize(pageSize);
        currentPage = getCurrentPage(currentPage);
        DSysUser currentUser = currentUser();
        BusinessMessage message = appInfoService.list(pageSize, currentPage, appName, enableFlag, currentUser);

        sysLogService.save(currentUser, LOG_OPERATION_QUERY, IPUtil.getIpAddr(request),
                "查询注册应用列表", message.getSuccess() ? 1 : 0, message.getMsg());
        StringBuffer url = new StringBuffer("/app/list?pageSize=10");
        if (!StringUtils.isEmpty(appName)) {
            url.append("&appName=" + appName);
            model.addAttribute("appName", appName);
        }
        if (!StringUtils.isEmpty(enableFlag)) {
            url.append("&enableFlag=" + enableFlag);
            model.addAttribute("enableFlag", enableFlag);
        }
        model.addAttribute("url", url);
        model.addAttribute("currentUser", currentUser);
        model.addAttribute("menus", listMenu());
        model.addAttribute("data", message.getData());
        return "pages/app/appInfoList";
    }

    /**
     * 跳转新增/编辑页面
     *
     * @param model
     * @param id
     * @return
     */
    @RequestMapping(value = "/add")
    public String addRole(Model model, String id) {
        AppInfo appInfo = new AppInfo();
        if (!StringUtils.isEmpty(id)) {
            appInfo = appInfoService.getAppInfoById(id);
        }
        model.addAttribute("appInfo", appInfo);
        model.addAttribute("currentUser", currentUser());
        model.addAttribute("menus", listMenu());
        return "/pages/app/appInfoAdd";
    }

    @RequestMapping(value = "/save")
    @ResponseBody
    public BusinessMessage saveRole(HttpServletRequest request, Model model, AppInfo appInfo) {
        BusinessMessage message = appInfoService.saveAppInfo(appInfo, currentUser());
        //保存操作日志
        if (!StringUtils.isEmpty(appInfo.getId())) {
            sysLogService.save(currentUser(), LOG_OPERATION_SAVE, IPUtil.getIpAddr(request),
                    "保存注册应用信息", message.getSuccess() ? 1 : 0, message.getMsg());
        } else {
            sysLogService.save(currentUser(), LOG_OPERATION_UPDATE, IPUtil.getIpAddr(request),
                    "更新注册应用[" + appInfo.getAppName() + "]信息", message.getSuccess() ? 1 : 0, message.getMsg());
        }
        message.setUrl("/app/info/list");
        return message;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public BusinessMessage delete(HttpServletRequest request, String id) {
        BusinessMessage message = appInfoService.deleteById(id);
        sysLogService.save(currentUser(), LOG_OPERATION_DEL,
                IPUtil.getIpAddr(request), "删除注册应用记录", message.success ? 1 : 0, message.getMsg());
        return message;
    }

    @RequestMapping("/checkAppName")
    @ResponseBody
    public BusinessMessage checkAppName(HttpServletRequest request, String appName, String id) {
        BusinessMessage message = appInfoService.checkAppName(appName, id);
        return message;
    }

}
